package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.PmsCategoryEntity;
import com.atguigu.gulimall.product.service.PmsCategoryService;
import com.atguigu.gulimall.product.vo.web.Catalog2Vo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    private PmsCategoryService categoryService;

    @GetMapping({"/", "index"})
    @ApiOperation(value = "查询出一级分类、渲染首页")
    public String index(Model model) {
        List<PmsCategoryEntity> categoryEntityList = categoryService.getTopCategory();
        model.addAttribute("category", categoryEntityList);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    @ApiOperation(value = "首页三级分类点击自动渲染", notes = "首页渲染完毕后、AJAX调用该接口")
    public Map<String, List<Catalog2Vo>> categoryTree() {
        Map<String, List<Catalog2Vo>> val = categoryService.listCategoryTree();
        return val;
    }

}

